package com.cmccgz.udacs_landlord_webapp.main.accreditmanager;

import android.content.Intent;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.cmccgz.baselibrary.base.BaseActivity;
import com.cmccgz.baselibrary.utils.ToastUtils;
import com.cmccgz.baselibrary.view.MyDividerItemDecoration;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.adapter.PersonListAdapter;
import com.cmccgz.udacs_landlord_webapp.base.AppConfig;
import com.cmccgz.udacs_landlord_webapp.bean.Person;
import com.cmccgz.udacs_landlord_webapp.interfaces.OnRvItemClickListenter;
import com.cmccgz.udacs_landlord_webapp.update.StringUtils;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

/**
 * @author: June
 * @Descripion:
 * @date 2017/10/27 09:09
 */

public class PersonListActivity extends BaseActivity implements PersonListContract.PersonListView, XRecyclerView.LoadingListener, View.OnClickListener, OnRvItemClickListenter<Person> {

    public static final String HOUSE_NAME = "house_name";
    public static final String HOUSE_ID = "house_id";
    public static final String KEY_PERSON_RESULT = "key_person_result";
    public static final int REQUEST_CODE = 0X11;

    private TextView tv_title;
    private TextView tv_back;
    private TextView tv_right;
    private XRecyclerView rv_list;
    private String houseId;
    private String houseName;

    private PersonListAdapter adapter;
    private TextView tv_houseName;
    private PersonListPresenter mPresenter;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_person_list;
    }

    @Override
    protected void initView() {
        initData();
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_back = (TextView) findViewById(R.id.tv_back);
        tv_back.setVisibility(View.VISIBLE);
        tv_right = (TextView) findViewById(R.id.tv_right);
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setText("新增");
        rv_list = (XRecyclerView) findViewById(R.id.rv_list);
        tv_title.setText("人员列表");
        tv_houseName = ((TextView) findViewById(R.id.tv_houseName));
        tv_houseName.setText(StringUtils.isEmpty(houseName)?"":houseName);
        initListener();
        initRecyclerView();
        mPresenter = new PersonListPresenter(this);
        refreshList(true);
    }

    private void refreshList(boolean isShowDialog) {
        if (isShowDialog) {
            showLoadingDialog("正在加载数据！");
        }
        mPresenter.getData(AppConfig.getMobile(mContext),houseId);
    }

    private void initListener() {
        tv_back.setOnClickListener(this);
        tv_right.setOnClickListener(this);
    }

    private void initRecyclerView() {
        adapter = new PersonListAdapter(mContext);
        adapter.setListenter(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_list.setLayoutManager(layoutManager);
        rv_list.setAdapter(adapter);
        rv_list.setRefreshProgressStyle(ProgressStyle.SysProgress);
        rv_list.setLoadingMoreProgressStyle(ProgressStyle.Pacman);
        rv_list.setLoadingListener(this);
        rv_list.addItemDecoration(new MyDividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
    }

    private void initData() {
        Intent intent = getIntent();
        houseId = intent.getStringExtra(HOUSE_ID);
        houseName = intent.getStringExtra(HOUSE_NAME);
    }

    @Override
    protected void initFragment() {

    }

    @Override
    protected void beforePermission() {

    }

    @Override
    protected int setSystemBarColorId() {
        return 0;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE&&resultCode==RESULT_OK){
            refreshList(true);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_right:
                Intent intent = new Intent(this, AddPersonActivity.class);
                intent.putExtra(HOUSE_ID, houseId);
                intent.putExtra(HOUSE_NAME, houseName);
                startActivityForResult(intent, REQUEST_CODE);
                break;
            default:
                break;
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void refreshDataSuccess(List<Person> persons) {
        dismissLoadingDialog();
        rv_list.refreshComplete();
        adapter.changeData(persons);
    }

    @Override
    public void refreshDataFailed(String errorMsg) {
        ToastUtils.show(mContext,errorMsg);
    }

    @Override
    public void setPresenter(PersonListContract.PersonListPresenter presenter) {
        
    }

    @Override
    public void onRefresh() {
        refreshList(false);
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onItemClick(Person person, int position, View itemView) {
        Intent data = new Intent();
        data.putExtra(KEY_PERSON_RESULT,person);
        setResult(RESULT_OK, data);
        onBackPressed();
    }
}
